#define CATCH_CONFIG_MAIN

#include "catch.hpp"
#include "random.hpp"
#include <map>
using namespace std;

TEST_CASE("Testing the set-random-sampler...", "[single-file]")
{
    srand((unsigned)time(NULL));

    map<int, int> counts;
    int64_t M = 2;
    int64_t N = 100000;
    int64_t SIZE = 200;
    int64_t MID = (M * N / SIZE);
    int64_t LOW = (M * N / SIZE) * 0.8;
    int64_t HIGH = (M * N / SIZE) * 1.2;
    for (int64_t j = 0; j < N; j++)
    {
        int64_t *ret = sample_index(SIZE, M);
        for (int64_t i = 0; i < M; i++)
        {
            if (counts.find(ret[i]) == counts.end())
            {
                counts[ret[i]] = 1;
            }
            else
            {
                counts[ret[i]] += 1;
            }
        }
        free(ret);
    }

    for (map<int, int>::iterator i = counts.begin(); i != counts.end(); ++i)
    {
        cout << (*i).first << ": " << (*i).second << endl;
        REQUIRE((*i).second <= HIGH);
        REQUIRE((*i).second >= LOW);
    }
}

TEST_CASE("Testing the reservoir-random-sampler...", "[single-file]")
{
    srand((unsigned)time(NULL));

    map<int, int> counts;
    for (int64_t j = 0; j < 5000; j++)
    {
        int64_t *ret = sample_index(10, 6);
        for (int64_t i = 0; i < 6; i++)
        {
            if (counts.find(ret[i]) == counts.end())
            {
                counts[ret[i]] = 1;
            }
            else
            {
                counts[ret[i]] += 1;
            }
        }
        free(ret);
    }
    for (map<int, int>::iterator i = counts.begin(); i != counts.end(); ++i)
    {
        cout << (*i).first << ": " << (*i).second << endl;
        REQUIRE((*i).second <= 3200);
        REQUIRE((*i).second >= 2800);
    }
}